package en_test

import (
	"bufio"
	// run `go env -w GOEXPERIMENT=jsonv2` to enable
	// the experimental jsonv2 support
	"encoding/json/v2"
	"fmt"
	"os"
	"sync"
	"testing"

	"gitcode.com/OpenDictionary/wiktionary2sql/schemas/en"
)

const MAX_WORKERS = 64

func enSchemaParallelTest(t *testing.T, file_path string) {
	var flag bool
	{
		dir, _ := os.Getwd()
		fmt.Println("Current wkdir is:", dir)
	}
	file, err := os.Open(file_path)
	if err != nil {
		t.Fatal(err)
	}
	defer file.Close()

	channel := make(chan []byte, MAX_WORKERS*4)
	var wg sync.WaitGroup

	// 启动 worker goroutines
	for i := 0; i < MAX_WORKERS; i++ {
		wg.Add(1)
		go func(workerID int) {
			defer wg.Done()
			for line := range channel {
				hashmap := make(map[string]any)
				if err := json.Unmarshal(line, &hashmap); err != nil {
					// t.Logf("Worker %d: Error unmarshaling JSON to a map[string]interface{}: %v\n\t\tline: %s", workerID, err, string(line))
					continue // 继续处理下一个
				}
				if lang_code, ok := hashmap["lang_code"]; !ok {
					if lang, ok := hashmap["lang"]; !ok {
						// t.Logf("Cannot find lang or lang_code in hashmap!\n")
						continue // 继续处理下一个
					} else if lang != "English" {
						// t.Logf("Skip non-English word: %s\n", lang_code)
						continue // 继续处理下一个
					}
				} else if lang_code != "en" {
					// t.Logf("Skip non-English word: %s\n", lang_code)
					continue // 继续处理下一个
				}

				// Deserialize the line with strict unmarshaling
				var word_data en.WordData

				if err := json.Unmarshal(line, &word_data); err != nil {
					flag = true
					t.Logf("Worker %d: Error unmarshaling JSON: %v\n", workerID, err)
					continue // 继续处理下一个
				}
				// 可选：处理 word_data
				// fmt.Printf("Worker %d processed word: %s\n", workerID, word_data.Word)
			}
		}(i)
	}

	// Read one line at a time
	scanner := bufio.NewScanner(file)
	scanner.Buffer([]byte{}, 1024*1024*100)
	for scanner.Scan() {
		line := scanner.Text()
		channel <- []byte(line)
	}
	if err := scanner.Err(); err != nil {
		t.Fatal(err)
	}

	// 关闭 channel 以通知 goroutines 没有更多数据
	close(channel)

	// 等待所有 goroutines 完成
	wg.Wait()
	if flag {
		// At least one error occurred during unmarshaling JSON
		t.Fatal("Error unmarshaling JSON")
	}
}

func TestEnSchemaParallel(t *testing.T) {
	var path = "./../../test_files/all/raw-wiktextract-data.jsonl"
	enSchemaParallelTest(t, path)
}
